package com.zhuchaooasystem.adapter.holder;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.ui.common.ImagePagerActivity;
import com.zhuchaooasystem.ui.common.LocalAlbumActivity;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.ScreenUtils;
import com.zhuchaooasystem.utils.ToastUtil;

/**
 * @author yinsi
 */
public class LocalAlbumHolder extends BaseViewHolder<String> {
    //----
    private RelativeLayout rootView;
    private RelativeLayout relative_checkbox;
    private ImageView iv_cover;
    private ImageView iv_check;

    //----
    private LocalAlbumActivity activity;
    private Context mContext;
    private int image_height;

    public LocalAlbumHolder(ViewGroup parent, LocalAlbumActivity activity) {
        super(parent, R.layout.item_activity_local_album_image);
        this.activity = activity;
        mContext = getContext();
        //----
        rootView = $(R.id.item_act_local_album_image_relative_rootview);
        relative_checkbox = $(R.id.item_act_local_album_image_relative_checkbox);
        iv_cover = $(R.id.item_act_local_album_image_iv_cover);
        iv_check = $(R.id.item_act_local_album_image_iv_check);
        //----
        image_height = (ScreenUtils.getScreenWidth((Activity) mContext) - ScreenUtils.dip2px(mContext, 20)) / activity.getNumColomn();
        iv_cover.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, image_height));
    }

    @Override
    public void setData(final String image) {
        //解决上下滑动checkbox错乱问题
        iv_check.setTag(getDataPosition());
        if (null != activity.getCheckPositionlist()) {
            if (activity.getCheckPositionlist().contains(getDataPosition())) {
                iv_check.setImageResource(R.drawable.ic_oval_checked);
            } else {
                iv_check.setImageResource(R.drawable.ic_oval_uncheck);
            }
        }
        //----
        Glide.with(mContext).load(image)
                .asBitmap().error(R.drawable.ic_image_default).diskCacheStrategy(DiskCacheStrategy.NONE).centerCrop().into(iv_cover);

        //----
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("images", activity.getImageList());
                bundle.putBoolean("isCache", false);
                bundle.putInt("position", getDataPosition());
                AppUtils.jump(mContext, ImagePagerActivity.class, bundle);
            }
        });
        relative_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置图片路径选中不选中的添加或移除
                if (activity.getImageChoosedList().contains(image)) {
                    activity.getImageChoosedList().remove(activity.getImageChoosedList().indexOf(image));
                    activity.getCheckPositionlist().remove(activity.getCheckPositionlist().indexOf(iv_check.getTag()));
                    iv_check.setImageResource(R.drawable.ic_oval_uncheck);
                } else {
                    if (activity.getImageChoosedList().size() + activity.getImage_cur() < activity.getImage_max()) {
                        activity.getImageChoosedList().add(image);
                        activity.getCheckPositionlist().add((Integer) iv_check.getTag());
                        iv_check.setImageResource(R.drawable.ic_oval_checked);
                    } else {
                        ToastUtil.showShort(activity, "最多上传" + activity.getImage_max() + "张图片");
                    }
                }
                //设置确定按钮
                if (null != activity.getImageChoosedList() && activity.getImageChoosedList().size() > 0) {
                    activity.setTxtConfirm(activity.getImageChoosedList().size());
                } else {
                    activity.setTxtConfirm(0);
                }
            }
        });

    }

}
